package com.android.lovely.ui.fragment

import androidx.core.content.ContentProviderCompat.requireContext
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.android.lovely.R
import com.android.lovely.const.ChatChange
import com.android.lovely.const.ChatList
import com.android.lovely.data.TransactionListBean
import com.android.lovely.databinding.FragmentHomeListBinding
import com.android.lovely.framework.BaseLoadAdapter
import com.android.lovely.framework.BaseLoadFragment
import com.android.lovely.framework.BaseLoadViewModel
import com.android.lovely.framework.widget.FlexibleContentContainer
import com.android.lovely.ui.adapter.TansAdapter
import com.android.lovely.ui.viewmodel.SearchViewModel
import com.android.lovely.ui.viewmodel.TransViewModel
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.flowOf
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import kotlinx.coroutines.flow.collectLatest

/**
 *
 * @author why
 * @date 2025/3/13 17:25
 */
open class TransListFragment :
    BaseLoadFragment<FragmentHomeListBinding, TransactionListBean>() {
    private val mViewModel by activityViewModels<TransViewModel>()

    val viewModel by activityViewModels<TransViewModel>()
    override fun getViewModel(): BaseLoadViewModel<TransactionListBean> {
        return mViewModel
    }

    override fun initView() {
        mBinding.rv.layoutManager = LinearLayoutManager(requireContext())
        super.initView()
        lifecycleScope.launchSafety {
            mViewModel.update.collectLatest {
                getSwipeRefreshLayout().isRefreshing = true
                getRecyclerView().scrollToPosition(0)
                adapter.refresh()
            }
        }
    }

    override fun setNoContent() {
        getStateLayout().setNoContentIcon(R.drawable.icon_no_content)
        getStateLayout().setNoContentText(getString(R.string.no_trans))
        getStateLayout().setNoContentButton(getString(R.string.research), false) {
            adapter.refresh()
        }
    }

    override fun getStateLayout(): FlexibleContentContainer {
        return mBinding.flexState
    }

    override fun getSwipeRefreshLayout(): SwipeRefreshLayout {
        return mBinding.srlRefresh
    }

    override fun getRecyclerView(): RecyclerView {
        return mBinding.rv
    }

    override fun getBaseAdapter(): BaseLoadAdapter<TransactionListBean, *> {
        return TansAdapter()
    }

    override suspend fun initData() {
        FlowBus.flowOf<ChatChange>().collectLatest {
            mBinding.srlRefresh.isRefreshing = true
            adapter.refresh()
        }
    }
}